
<script>
function clickTimKiem()
	{
		if(document.getElementById ('formTimKiem').style.display == 'none')
		{
			document.getElementById ('formTimKiem').style.display = 'block';
		}
		else
		{
			document.getElementById ('formTimKiem').style.display = 'none';
		}
	}

</script>
<?php
 function word_split($str,$words) {
	$arr = preg_split("/[\s]+/", $str,$words+1);
	$arr = array_slice($arr,0,$words);
	return join(' ',$arr);
 }
//define a maxim size for the uploaded images in Kb
 define ("MAX_SIZE","300"); 

//This function reads the extension of the file. It is used to determine if the
// file  is an image by checking the extension.
	 function getExtension($str) {
			 $i = strrpos($str,".");
			 if (!$i) { return ""; }
			 $l = strlen($str) - $i;
			 $ext = substr($str,$i+1,$l);
			 return $ext;
	 }
		$errors=0;
		 if(isset($_POST['Submit'])) 
		 {
			$image=$_FILES['image']['name'];
			$tenfile = $_POST["tenfile"];
			if ($image) 
			{
				$filename = stripslashes($_FILES['image']['name']);
				$extension = getExtension($filename);
				$extension = strtolower($extension);
				 if (($extension != "doc") && ($extension != "docx") && ($extension !="xlsx") && ($extension != "xls") && ($extension != "pdf") )
				 {
							echo "<script>alert('Không đúng định dạng file');</script>";
							$errors=1;
				  }
				  else
				  {
							 $size=filesize($_FILES['image']['tmp_name']);
							if ($size > MAX_SIZE*1024)
							{
								echo "<script>alert('Kích thước file không được quá 300KB');</script>";
								$errors=1;
							} else 
							{
								$tenfile = $image;
								$newname="../upload/files/lichcongtac/".$image;
								$copied = copy($_FILES['image']['tmp_name'], $newname);
								if (!$copied) 
								{
									echo '<h1>Up file lên server lỗi</h1>';
									$errors=1;
								}
							}
				  }
			}			
		 }
?>
	<div id="menu_tool">
    	<div id="tool_icon">
        	<ul>
            	<li><a href="?go=addschedule"><img src="img/add.png" /></a></li>
            </ul>
            <ul>
                <li style="padding:0 5px 0 0px"><a href="?go=addschedule">Thêm Mới</a></li>
            </ul>
        </div>
    </div>	
    <?									
				$action = $_REQUEST['action']; 			
					switch($action) {
						case "search":
						{							
							if (!isset($_REQUEST['tieude']) && !isset($_REQUEST['trangthai'])) {
								$tieude = $_SESSION['tieude'];	
								$trangthai = $_SESSION['trangthai'];
							} else {
								$tieude = $_REQUEST['tieude'];
								$trangthai = $_REQUEST['trangthai'];
								$_SESSION['tieude'] = $tieude;
								$_SESSION['trangthai'] = $trangthai;
							}
							$sqlSearch = " WHERE tieude LIKE '%$tieude%'";
							if ($trangthai < 2) { 
								$sqlSearch .= " AND status = '$trangthai' ";
							}
							$_SESSION["search"] = $sqlSearch;
						}
						break;
						
						case "view":
						{
							$_SESSION["search"] = "";
						}
						break;
						
						case 'add':
						{
							$tieude = $_REQUEST['tieude'];
					//		$thoigian = $_REQUEST['thoigian'];
							if ($errors == 1) {
									echo "<script>alert('Upload file lỗi'); window.history.go(-1);</script>";
									
							} else {
							$luutru = $image;
							$status = $_REQUEST['trangthai'];
							$sql="insert lichcongtac(tieude, thoigian, luutru, status) values('$tieude', now() , '$luutru', $status)";
							$result=mysql_query($sql) or die("Can not insert lichcongtac " .mysql_error($conn));
							echo "<script> location='?go=schedule&action=view&page=1';</script>";
								}
						}
						break;
						
						case "del":
						{
								$id = $_REQUEST['id'];
								$sql="select * from lichcongtac where idlichcongtac= $id";
								$result=mysql_query($sql) or die("Can not query delete lichcongtac".mysql_error($conn));
								$row=mysql_fetch_array($result);
								if(mysql_num_rows($result)!= 0)
								{
									$sql="delete from lichcongtac where idlichcongtac = $id";
									$result=mysql_query($sql) or die("Can not delete lichcongtac".mysql_error($conn));
								}
								else
								{
									echo "<script> alert('Không thể xóa thông báo'); </script>";
								}
								echo "<script>alert('Xóa thành công'); location='?go=schedule&action=view&page=1';</script>";
						}
						break;
						
						case "edit":
						{
								$id = $_REQUEST['id'];
								$tieude = $_REQUEST['tieude'];
						//		$thoigian = $_REQUEST['thoigian'];
								if ($errors == 1) {
											echo "<script>alert('Upload file lỗi');window.history.go(-1);</script>";
									} else {
								$luutru = $tenfile;
								$status = $_REQUEST['trangthai'];
								$sql="select * from lichcongtac where tieude ='$tieude' and idlichcongtac !=$id";
								$result=mysql_query($sql) or die("Can not query lichcongtac in edit");
								if(mysql_num_rows($result)!=0)
									echo "<script>alert('Tiêu đề đã tồn tại, nhập tiêu đề khác'); window.history.go(-1);</script>";
								else
								{
									$sql="update lichcongtac set tieude ='$tieude',status = $status,luutru = '$luutru' where idlichcongtac =$id";
									$result=mysql_query($sql) or die("Can not update lichcongtac".mysql_error($conn));
									echo "<script>alert('Sửa thành công');</script>";		
								}
								echo "<script> location='?go=schedule&action=view&page=1';</script>";
						}
						}
						break;
					}
			
		?>
     
        <div id="bang">
    	<table border="1px"  bordercolor="#CCCCCC" cellpadding="0px" cellspacing="0px" cols="8" width="790px">
        		<tr>
				<td colspan="10" align="left"> &nbsp;<input type="button" value="Lọc Dữ Liệu" id="TimKiem" onclick="clickTimKiem()" style="padding:0 5px" /> 
				<form name="formTimKiem" id="formTimKiem" method="post" action="?go=schedule&action=search&page=1" style="display:none">
				<br />
					<table cellpadding="0" cellspacing="0" align="center" width="100%" border="0">
						<tr>
							<td width="40%" align="right"> Tiêu đề : &nbsp;</td>
							<td width="60%" align="left"> &nbsp;<input type="text" name="tieude"  /> </td>
						</tr>
						<tr>
							<td align="right"> Trạng thái : &nbsp;</td>
							<td> &nbsp;<select name="trangthai">
								<option value = "2" selected="selected">Tất cả</option>
								<option value="1" >Hoạt động</option>
								<option value="0">Không hoạt động</option>
					
       			 				</select></td>
						</tr>
						<tr>
							<td colspan="2" align="center"> <input type="submit" value="Lọc dữ liệu" style="padding:0 5px" /></td>
						</tr>
					</table>
				<br />
				</form>
				</td>
              
			</tr>
           <!-- <tr>
            	<td colspan="4"></td>
            	<td colspan="2"><input type="button" id="themmoi" value="Thêm Mới" style="padding: 0 5px"  /></td>
                <td colspan="2"><input type="button" id="kichhoat" value="Kích Hoạt" style="padding: 0 5px"  /></td>
                <td colspan="2"><input type="button" id="vohieu" value="Vô Hiệu" style="padding: 0 5px"  /></td>                
            </tr>-->
			
            <th style="max-width:30px">STT</th>
            <th style="max-width:300px">Tiêu Đề</th>
            <th style="max-width:80px">Lưu Trữ</th>
            <th style="max-width:40px">Trạng Thái<th>
            <th style="max-width:120px">Ngày Tạo</th>
            <th style="max-width::30px">Sửa</th>
 			<th style="max-width:30px">Xóa</th>
            <th style="max-width:80px">Kích Hoạt</th>
            <th style="max-width:80px">Vô Hiệu</th>
        <?
			$i = 0;
			$display  = 10;
			$pagenum = $_REQUEST["page"];
			$sql = "SELECT * FROM lichcongtac";
			$sql .= $_SESSION["search"];
			$queryCount = mysql_query($sql) or die("Can not query count lichcongtac");
			$count = mysql_num_rows($queryCount);
			$sql .= " ORDER BY thoigian DESC";
			$result=mysql_query($sql) or die("Can not query lichcongtac Order".mysql_error($conn));
				$totalpage=ceil(mysql_num_rows($result)/$display);
				if($pagenum<=0 ||$pagenum > $totalpage)
					$pagenum=1;
				$from=($pagenum-1)*$display;
				$sql.="  LIMIT $from, $display";
				$query=mysql_query($sql)  or die("Can not query lichcongtac Limit".mysql_error($conn));;	
			while ($row=mysql_fetch_array($query)) 
			{			
				$temp = $row['idlichcongtac'];
				$i++;
		?>
        	<tr>
            	<td class="stt"><? echo ($from+$i); ?></td>
                <td class="tieude" title="<?=$row['tieude']?>"><?php echo  word_split($row['tieude'],10);?></td>
                <td class="anhpreview" title="<?=$row['luutru']?>"><? echo word_split($row['luutru'],10); ?> </td>
                <td class="trangthai" align="center"><? if ($row['status'] == 1) {
					echo '<img src="img/Check.jpg"/>';	
				} else {
					echo '<img src="img/Delete.jpg"/>';
				}
				?>
				</td>
                <td></td>
                 <td class="thoigian" style="max-width:120px"><?php echo ($time = date('d-m-Y G:i:s',strtotime($row['thoigian'])));?></td>
                <td class="chucnang" align="center"> <input type="button" value="Sửa" onClick="location='?go=editschedule&id=<?php echo $temp; ?>';"  style="width:100%"/></td>
				<td align="center"> <input type="button" value="Xoá" onClick="if (confirm('Bạn chắc chắn muốn xóa?') == true)
				{
					location='?go=schedule&action=del&id=<?php echo $temp; ?>';			
				}
				else
				return false;"  style="width:100%"/></td>
        		<td class="chucnang" align="center"> <input type="button" value="Kích Hoạt" onClick="if (confirm('Bạn muốn kích hoạt?') == true) {
                	location='?go=sttschedule&action=enable&id=<?php echo $temp; ?>';
                } else {
                	return false;
                }" style="width:90%" /></td>
                <td class="chucnang" align="center"> <input type="button" value="Vô Hiệu" onClick="if (confirm('Bạn muốn vô hiệu hóa?') == true) {
                location='?go=sttschedule&action=disable&id=<?php echo $temp; ?>';
                } else return false;"  style="width:90%" /></td>        
            </tr>
        <?		
			} 
			//end while 

			
			?>
             <tr>
				<td colspan="3">Tổng số bản ghi: <? echo $count ?> </td>
                <td colspan="7">Số bản ghi/trang: <? echo $display ?></td>
                </td>
			</tr>
            <tr class="pagelist">
            	<td colspan="10">
                <? 
				for($i=1;$i<=$totalpage;$i++)
				{
					if ($action == 'view') {
					if($i==1)
						echo "<a href='?go=schedule&action=view&page=$i'>".$i."</a>";
					else
						echo " | <a href='?go=schedule&action=view&page=$i'>".$i."</a>";
				
				} else {
					if($i==1)
						echo "<a href='?go=schedule&action=search&page=$i'>".$i."</a>";
					else
						echo " | <a href='?go=schedule&action=search&page=$i'>".$i."</a>";
				}
				}
                ?>
                </td>
             </tr>

  			
      </table>
          <? 
	
		  ?> 
    </div>
   
	<div class="clear"></div>
</div>
          
